﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Description;
using CdmDeviceHealthMvc.Models;
using SnShared.Models;

namespace CdmDeviceHealthMvc.Controllers
{
    public class DemoResetController : ApiController
    {
        private CdmDataCtx db = CdmDataCtx.CreateDbContext();

        // PUT: api/DemoReset/5
        [ResponseType(typeof(void))]
        public IHttpActionResult PutSoftwarePackage(int id, SoftwarePackage softwarePackage)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != softwarePackage.Id)
            {
                return BadRequest();
            }

            db.Entry(softwarePackage).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!SoftwarePackageExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return StatusCode(HttpStatusCode.NoContent);
        }

      

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }

        private bool SoftwarePackageExists(int id)
        {
            return db.SoftwarePackages.Count(e => e.Id == id) > 0;
        }
    }
}